/*
 * @Author: silence
 * @Date: 2023-07-10 16:42:28
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-26 17:44:27
 * @Description: 电站管理
 */
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:ems_neutral_app/common/interfaceInstance/FutureBuilderInterfaceInstance.dart';
import 'package:ems_neutral_app/pages/PowerStation/Add/PowerStationAddPage.dart';
import 'package:ems_neutral_app/pages/PowerStation/Manager/PsManagerListItem.dart';
import 'package:ems_neutral_app/utils/eventBus/eventBus.dart';
import 'package:ems_neutral_app/utils/router/ARouter.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../api/R.dart';
import '../../../common/widgets/cubitWidget/SubmitBtn.dart';
import '../../../utils/string/StringConstant.dart';
import '../../Mine/Language/SilLocalizations/SilLocalizations.dart';
import '../Controller/PowerStationController.dart';

class PowerStationManagerPage extends StatefulWidget {
  const PowerStationManagerPage({super.key});

  @override
  State<PowerStationManagerPage> createState() =>
      _PowerStationManagerPageState();
}

class _PowerStationManagerPageState extends State<PowerStationManagerPage>
    with FutureBuilderInterfaceInstance {
  PowerStationController _powerStationController =
      Get.put(PowerStationController());
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  dynamic _future;
  List _list = [];
  late EventCallback _eventCallback;

  @override
  void initState() {
    super.initState();
    _future = getData();

    _eventCallback = (arg) {
      setState(() {
        _future = getData();
      });
    };

    /// 电站数量放生了变化
    bus.on(StringConstant.PowerStationsNumChange, _eventCallback);
  }

  @override
  void dispose() {
    bus.off(StringConstant.PowerStationsNumChange, _eventCallback);
    super.dispose();
  }

  getData() async {
    R r = await _powerStationController.getPowerStationList(context,
        apiShowHandler: false);
    return r.data;
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      title: LangOf(context)?.userDz_setting?.dz_system,
      sideMargin: 0,
      body: contentView(),
    );
  }

  contentView() {
    return SilFutureBuilderWidget(
        refreshController: _refreshController,
        future: _future,
        mainWidget: (data) {
          _list = data;
          return mainWidget();
        },
        reloadFunc: () {
          getData();
          setState(() {});
        },
        interface: this);
  }

  mainWidget() {
    return CustomScrollView(
      slivers: [
        SliverToBoxAdapter(
          child: listView(),
        ),

        /// 添加电站按钮
        SliverToBoxAdapter(
          child: ConstrainedBox(
            constraints: BoxConstraints(minWidth: 180.w),
            child: SubmitBtn(
              title: LangOf(context)?.userDz_setting?.add,
              onTapFunc: () {
                ARoute.gotoPage(context, PowerStationAddPage());
              },
            ),
          ),
        )
      ],
    );
  }

  listView() {
    return GridView.builder(
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 15.w),
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 30.w,
          mainAxisSpacing: 20.w,
          childAspectRatio: 326.0 / 385),
      itemBuilder: (context, index) {
        return PsManagerListItem(deviceEntity: _list[index]);
      },
      itemCount: _list.length,
    );
  }
}
